
from Base import *
from Header import *

class CVersionElement(CInfoElement):
    _gName = "Version Element"
    _gNameCarManId = "Car Manufacturer ID"
    _gNameDcmManId = "DCM Manufacturer ID"
    _gNameHwRel = "DCM Major HW"
    _gNameSwRel = "DCM Major SW"


    _gInfoLength = 4 #version element length is 4 byte(not include element header)
    _gCarManId = 0x0C
    _gDcmManId = 0x80
    _gHwRel = 0x01
    _gSwRel = 0x01

    def __init__(self, name=_gName):
        super(CVersionElement, self).__init__(name)
        self._hlColor = 'black_on_green'

    def InitSubItem(self):
        name = self._gNameHeader
        item = CEleHeader(name, recommendedLen = self._gInfoLength)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameCarManId
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameDcmManId
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameHwRel
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameSwRel
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)



    def ParseElement(self, dataStr):
        #version element has 4 byte item
        self._carManufacturerID = dataStr.GetByte()
        item = self._subList[self._gNameCarManId]
        item._rawStr = HEX_DATA_FORMAT.format(self._carManufacturerID)
        if self._carManufacturerID != self._gCarManId:
            item._errStr = "Invalid Car Manufacturer"

        self._dcmManufacturerID = dataStr.GetByte()
        item = self._subList[self._gNameDcmManId]
        item._rawStr = HEX_DATA_FORMAT.format(self._dcmManufacturerID)
        if self._dcmManufacturerID != self._gDcmManId:
            item._errStr = "Invalid DCM Manufacturer"

        self._hwRelease = dataStr.GetByte()
        item = self._subList[self._gNameHwRel]
        item._rawStr = HEX_DATA_FORMAT.format(self._hwRelease)
        if self._hwRelease != self._gHwRel:
            item._errStr = "Invalid HW Release"

        self._swRelease = dataStr.GetByte()
        item = self._subList[self._gNameSwRel]
        item._rawStr = HEX_DATA_FORMAT.format(self._swRelease)
        if self._swRelease != self._gSwRel:
            item._errStr = "Invalid SW Release"
        

